<?php defined('BASEPATH') OR exit('No direct script access allowed');

class Online extends Admin_Controller
{
    function __construct()
    {
        parent::__construct();
        $this->load->library('pagination');
        $this->load->model('online_m');
        $this->load->model('OnlineCategory_m');
    }

    public function index($page = 1)
    {
        $get    = $this->input->get();
        $search = $where = [];
        if (isset($get['online_title']) && !empty($get['online_title'])) {
            $search['keyword'] = $keyword = $get['online_title'];
            $where             = "title like '%{$keyword}%'";
        }
        //分页
        $limit                      = 20;
        $config['uri_segment']      = 4;
        $config['use_page_numbers'] = true;
        $config['total_rows']       = $this->online_m->total($where);
        $config['base_url']         = base_url('manage/Online/index');
        $config['per_page']         = $limit;
        $config['num_links']        = 1;
        $start                      = ($page - 1) * $limit;
        $this->pagination->initialize($config);
        $data['pagination'] = $this->pagination->create_links();
        $onlines           = $this->online_m->select($where, $limit, $start);
        if ($onlines) {
            foreach ($onlines as $key => $val) {
                if (!empty($val['category'])) {
                    $category                   = $this->OnlineCategory_m->find($val['category']);
                    $onlines[$key]['category'] = $category['category_name'];
                } else {
                    $onlines[$key]['category'] = "--";
                }
            }
        }
        $data['search']   = $search;
        $data['onlines'] = $onlines;
        $this->load->view('online/index', $data);
    }

    public function add()
    {
        $data['pic_path'] = "[]";
        $data['pic_con']  = json_encode([]);
        $data['category'] = $this->OnlineCategory_m->select(['deleted' => 0]);
        $this->load->view('online/form', $data);
    }

    public function edit($online_id = null)
    {
        $online = $this->online_m->find($online_id);
        if (!$online) {
            show_message('参数错误！', site_url(X_URL . 'Online/index'));
        }

        $del_url = base_url("index.php/Upload/delete_img?url=" . $online['img']);;
        $pic_con[]          = [
            'caption' => "产品图",
            'size'    => 576237,
            'width'   => '120px',
            'url'     => $del_url,
            "key"     => $online['id'],
        ];
        $data['pic_path']   = "['" . $online['img'] . "']";
        $data['pic_con']    = json_encode($pic_con);
        $data['online']    = $online;
        $data['online_id'] = $online['id'];
        $data['category']   = $this->OnlineCategory_m->select(['deleted' => 0]);

        $this->load->view('online/form', $data);
    }

    public function del($online_id)
    {
        if ($online_id) {
            if ($this->online_m->delete_by(['id' => $online_id])) {
                show_message('恭喜，删除成功', site_url(X_URL . 'Online/index'), 1);
            } else {
                show_message('参数错误', site_url(X_URL . 'Online/index'));
            }
        } else {
            show_message('参数错误', site_url(X_URL . 'Online/index'));
        }
    }

    public function update()
    {

        $post          = $this->input->post();
        $online_title = isset($post['title']) ? htmlspecialchars($post['title'], ENT_QUOTES) : false;
        $online_id    = isset($post['online_id']) ? intval($post['online_id']) : false;
        $online       = [
            'title'    => $online_title,
            'category' => $post['category'] ?? 0,
            'author'   => $post['author'] ?? "",
            'img'      => $post['img'] ?? "",
            'content'  => $post['content'] ?? "",
            'keyword'  => $post['keyword'] ?? "",
            'describe' => $post['describe'] ?? "",
            'is_hot'   => $post['is_hot'] ?? 0,
        ];
        if (!$online_id) {
            // 新增
            $online['create_time'] = date("Y-m-d H:i:s", time());
            $onlines               = $this->online_m->insert($online);
            if ($onlines) {
                show_message('新增动态成功', site_url(X_URL . 'Online/index'), 1);
            } else {
                show_message('新增动态失败', site_url(X_URL . 'Online/add'));
            }
        } else {
            // 修改动态信息
            $affected_rows = $this->online_m->update(['id' => $online_id], $online);
            if ($affected_rows) {
                show_message('修改动态成功', site_url(X_URL . 'Online/index'), 1);
            } else {
                show_message('修改动态失败', site_url(X_URL . 'Online/edit'));
            }
        }
    }
}

